package com.shilei.proxy.staticProxy;

/**
 * @description:  静态代理
 *          问题：我想记录坦克移动的时间
 *          最简单的方法， 修改代码，记录时间
 *
 *          问题2：如果无法修改方法源码呢？
 *          用继承？
 *          使用代理  代理有各种类型
 *          问题：如何实现代理的各种组合？继承？ Decorator?
 *          代理的对象改成Movable类型 越来越像Decorator
 * @author: 81271
 * @date: 2020-04-18 18:38
 */
public class Tank implements Moveable{

    @Override
    public void move() {
        System.out.println("Tank running kakakakakakakaka");
    }
}

class TankTimeProxy implements Moveable{
    Moveable moveable;

    public TankTimeProxy(Moveable moveable) {
        this.moveable = moveable;
    }

    @Override
    public void move() {
        long start = System.currentTimeMillis();
        moveable.move();
        long end = System.currentTimeMillis();
        System.out.println("花费时间：" + (end - start));
    }
}

class TankLogProxy implements Moveable{
    Moveable moveable;

    public TankLogProxy(Moveable moveable) {
        this.moveable = moveable;
    }

    @Override
    public void move() {
        System.out.println("Tank start running");
        moveable.move();
        System.out.println("Tank end run");
    }
}

interface Moveable {
    void move();
}

class Main {
    public static void main(String[] args) {
        new TankLogProxy(
                new TankTimeProxy(
                        new Tank()
                )
        ).move();
    }
}
